package cn.usian.jyzx.cart.controller;

import cn.usian.jyzx.cart.service.CartService;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;


import cn.usian.jyzx.model.vo.h5.CartInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 11 06 10 07
 **/

@RestController
public class CartController {

    @Autowired
    CartService cartService;

    /**
     * skuId:商品SKUID
     * skuNum: 购买数量
     *
     * @return
     */
    @GetMapping("/api/order/cart/auth/addToCart/{skuId}/{skuNum}")
    public Result addCart(@PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum, HttpServletRequest request

                          ) {

        cartService.addCart(skuId, skuNum,request.getHeader("token"));

        return Result.build(null, ResultCodeEnum.SUCCESS);

    }

    /**
     * 查询购物车的接口
     */

    @GetMapping("/api/order/cart/auth/cartList")
    public  Result cartList(HttpServletRequest request){
        List<CartInfo> cartInfos = cartService.cartList(request.getHeader("token"));
        return Result.build(cartInfos, ResultCodeEnum.SUCCESS);


    }

    /**
     * 删除购物车
     */

    @DeleteMapping("/api/order/cart/auth/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable("skuId") Long skuId,HttpServletRequest request){
        cartService.deleteCart(skuId,request.getHeader("token"));
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改购物车状态
     * /api/order/cart/auth/checkCart/2/1"
     */

    @GetMapping("/api/order/cart/auth/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable("skuId") Long skuId,@PathVariable("isChecked") Integer isChecked,HttpServletRequest request){
        cartService.checkCart(skuId,isChecked,request.getHeader("token"));
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 全选/反选
     * /api/order/cart/auth/allCheckCart/1
     */

    @GetMapping("/api/order/cart/auth/allCheckCart/{isChecked}")
    public Result allCheckCart(@PathVariable("isChecked") Integer isChecked,HttpServletRequest request){
        cartService.allCheckCart(isChecked,request.getHeader("token"));
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 清空购物车
     * http://127.0.0.1:8081/api/order/cart/auth/clearCart
     */
    @DeleteMapping("/api/order/cart/auth/clearCart")
    public Result clearCart(HttpServletRequest request){
        cartService.clearCart(request.getHeader("token"));
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/api/order/cart/auth/getCartCheckedList")
    public Result getCartCheckedList(@RequestParam("token") String token){
         cartService.deleteCheckedCart(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
